package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.listvm.BankListVm;
import com.typy.www.tpay.bean.listvm.BuyConfigListVm;
import com.typy.www.tpay.bean.vm.BankCardVm;
import com.typy.www.tpay.bean.vm.BuyConfigVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.RadioAdapter;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.ui.prt.PtrScrollContent;
import com.typy.www.tpay.ui.view.XRadioGroup;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.ScollGridView;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.ToastUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/18.
 */

public class BuyIntoActivity extends BaseBackActivity {

    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.rb4)
    RadioButton rb4;
    @BindView(R.id.rb5)
    RadioButton rb5;
    @BindView(R.id.rb6)
    RadioButton rb6;
    @BindView(R.id.ll_addcard)
    LinearLayout llAddcard;
    //    @BindView(R.id.rbs1)
//    RadioButton rbs1;
//    @BindView(R.id.rbs2)
//    RadioButton rbs2;
//    @BindView(R.id.rbs3)
//    RadioButton rbs3;
//    @BindView(R.id.rbs4)
//    RadioButton rbs4;
//    @BindView(R.id.rbs5)
//    RadioButton rbs5;
//    @BindView(R.id.rbs6)
//    RadioButton rbs6;
    @BindView(R.id.publish)
    TextView publish;
    @BindView(R.id.rg_root1)
    XRadioGroup rgRoot1;
    //    @BindView(R.id.rg_root2)
//    XRadioGroup rgRoot2;
    @BindView(R.id.ll_gonggao)
    PercentLinearLayout llGonggao;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_bankName)
    TextView tvBankName;
    @BindView(R.id.tv_cardNum)
    TextView tvCardNum;
    @BindView(R.id.ll_bindcard)
    PercentLinearLayout llBindcard;
    @BindView(R.id.ll_card)
    LinearLayout llCard;
    @BindView(R.id.et_maichuremark)
    AutoCompleteTextView etMaichuremark;
    @BindView(R.id.gv_mine)
    ScollGridView gvMine;
    private BankCardVm bankCardVm;
    private int classType;
    private PtrScrollContent ptrScrollContent;
    private boolean haveCard = false;
    private BuyConfigListVm buyConfigListVm;
    private List<BuyConfigVm> configData;
    BuyConfigVm buyConfigVm;

    private RadioAdapter adapter;


    public static void show(Context context, int classType) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        IntentUtil.intent2Activity(context, intent, BuyIntoActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("classType", 0);
        initTitle();
        initRadio();
        initAdapter();

        getAmountitemlistData();
    }

    private void initAdapter() {
        adapter = new RadioAdapter(getActivity());
        adapter.setListener(new RadioAdapter.BuyConfigListener() {
            @Override
            public void getBuyConfig(BuyConfigVm vm) {
                buyConfigVm = vm;
            }
        });
        gvMine.setAdapter(adapter);
    }


    private void initTitle() {
        setTitle(ClassHoldUtil.getClassTitle(classType));
        if (classType == ClassHoldUtil.BUYIN) {
            rb3.setText("确认打款");
            rb5.setText("买入记录");
            rb6.setText("买入中心");
            etMaichuremark.setVisibility(View.GONE);
        } else {
            etMaichuremark.setVisibility(View.VISIBLE);
            etMaichuremark.clearFocus();
            rb3.setText("确认收款");
            rb5.setText("卖出记录");
            rb6.setText("卖出中心");
        }
    }

    private void initRadio() {
        rb1.setChecked(true);
        rb1.setTextColor(getResources().getColor(R.color.white));
        rgRoot1.setOnCheckedChangeListener(new XRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(XRadioGroup group, int checkedId) {
                if (checkedId == rb1.getId()) {
                    updateRb(rb1);
                } else if (checkedId == rb2.getId()) {
                    updateRb(rb2);
                    if (classType == ClassHoldUtil.BUYIN)
                        BuyCenterActivity.show(getActivity(), ClassHoldUtil.BUYWEIWANCHENG);
                    else
                        BuyCenterActivity.show(getActivity(), ClassHoldUtil.SELLWEIWANCHENG);
                } else if (checkedId == rb3.getId()) {
                    updateRb(rb3);
                    if (classType == ClassHoldUtil.BUYIN)
                        RecordActivity.show(getActivity(), ClassHoldUtil.QUERENGDAKUANB);
                    else
                        RecordActivity.show(getActivity(), ClassHoldUtil.QUERENGDAKUANS);
                } else if (checkedId == rb4.getId()) {
                    updateRb(rb4);
                    if (classType == ClassHoldUtil.BUYIN)
                        RecordActivity.show(getActivity(), ClassHoldUtil.BUYYIWANCHENG);
                    else
                        RecordActivity.show(getActivity(), ClassHoldUtil.SELLYIWANCHENG);
                } else if (checkedId == rb5.getId()) {
                    updateRb(rb5);
                    if (classType == ClassHoldUtil.BUYIN)
                        RecordActivity.show(getActivity(), ClassHoldUtil.BUYINCORECORD);
                    else
                        RecordActivity.show(getActivity(), ClassHoldUtil.SELLOUTRECORD);
                } else if (checkedId == rb6.getId()) {
                    updateRb(rb6);

                    if (classType == ClassHoldUtil.BUYIN)
                        BuyCenterActivity.show(getActivity(), ClassHoldUtil.BUYINCENTER, buyConfigVm.getFdAmountConfigId());
                    else
                        BuyCenterActivity.show(getActivity(), ClassHoldUtil.SELLOUTCENTER, buyConfigVm.getFdAmountConfigId());

                }
            }
        });
    }

    private void updateRb(TextView textView) {
        rb1.setTextColor(getResources().getColor(R.color.blue67BCFD));
        rb2.setTextColor(getResources().getColor(R.color.blue67BCFD));
        rb3.setTextColor(getResources().getColor(R.color.blue67BCFD));
        rb4.setTextColor(getResources().getColor(R.color.blue67BCFD));
        rb5.setTextColor(getResources().getColor(R.color.blue67BCFD));
        rb6.setTextColor(getResources().getColor(R.color.blue67BCFD));
        textView.setTextColor(getResources().getColor(R.color.white));
    }


    @Override
    protected View getContentView() {
        ptrScrollContent = new PtrScrollContent(getActivity(), R.layout.activity_buyin) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                getData();
            }
        };
        return ptrScrollContent.getmView();
    }


    @OnClick({R.id.ll_addcard, R.id.ll_gonggao, R.id.publish, R.id.ll_bindcard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_addcard:
                AddBankCardActivity.show(getActivity());
                break;
            case R.id.ll_gonggao:
                MyBankCardActivity.show(getActivity(), classType);
                break;
            case R.id.ll_bindcard:
                break;
            case R.id.publish:
                if (!haveCard) {
                    ToastUtil.getInstance().show("请添加银行卡");
                    return;
                }
                if (bankCardVm == null) {
                    ToastUtil.getInstance().show("请绑定银行卡");
                    return;
                }
                PwdDailogUtil.show(this, new InputCallBack() {
                    @Override
                    public void onInputFinish(String result) {
                        creatOrder(result);
                    }
                });
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        rb1.setChecked(true);
        updateRb(rb1);
        getData();
    }

    private void updateUi() {
        if (bankCardVm != null) {
            llCard.setVisibility(View.VISIBLE);
            UpdataUiUtil.getDefaul().setText(bankCardVm.getFdBankUserName(), tvNickname)
                    .setText(bankCardVm.getFdBankName(), tvBankName)
                    .setText("银行卡号：" + bankCardVm.getFdBankNo(), tvCardNum);
        } else {
            llCard.setVisibility(View.GONE);
        }
    }

    //获取数据
    private void getData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getBankdefaultURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {

                if (ret != null && ret.getData() != null) {
                    BankCardVm vm = GsonUtils.parseJSON(ret.getData().toString(), BankCardVm.class);
                    if (vm != null && !StringUtil.isEmpty(vm.getFdBankUserName())) {
                        llAddcard.setVisibility(View.GONE);
                        llBindcard.setVisibility(View.VISIBLE);
                        haveCard=true;
                        bankCardVm = vm;
                        updateUi();
                    } else {
                        haveCard=false;
                        llAddcard.setVisibility(View.VISIBLE);
                        llBindcard.setVisibility(View.GONE);
                    }

                }

                ptrScrollContent.loadInitialPage();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                ptrScrollContent.loadInitialPage();
            }
        });
    }

    private void getAmountitemlistData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getAmountitemlistURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret != null && ret.getData() != null) {
                    buyConfigListVm = GsonUtils.parseJSON(ret.getData().toString(), BuyConfigListVm.class);
                    if (buyConfigListVm != null && buyConfigListVm.getData() != null && buyConfigListVm.getData().size() > 0) {
                        configData = buyConfigListVm.getData();
                        configData.get(0).setCheck(true);
                        buyConfigVm = configData.get(0);
                        adapter.setBuyConfigList(configData);
                        adapter.notifyDataSetChanged();
                    }
                }
                ptrScrollContent.loadInitialPage();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                ptrScrollContent.loadInitialPage();
            }
        });
    }

    //创建订单
    private void creatOrder(String pwd) {
        ParamsUtil params = new ParamsUtil();
        params.put("fdUserBankId", bankCardVm.getFdId());
        params.put("fdAmountConfigId", buyConfigVm.getFdAmountConfigId());
        params.put("fdRemark", etMaichuremark.getText().toString());
        params.putMd5("fdPayPwd", pwd);
        String url = "";
        if (classType == ClassHoldUtil.BUYIN)
            url = Constance.getCreatebuyURL();
        else
            url = Constance.getCreatesellerURL();
        HTTPUtils.post(getActivity(), url, params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "订单创建成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        if (classType == ClassHoldUtil.BUYIN)
                            BuyCenterActivity.show(getActivity(), ClassHoldUtil.BUYWEIWANCHENG);
                        else
                            BuyCenterActivity.show(getActivity(), ClassHoldUtil.SELLWEIWANCHENG);
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
            }
        });
    }


}
